package com.ysx.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysx.admin.mapper.AdUserMapper;
import com.ysx.admin.pojo.AdUser;
import com.ysx.admin.service.AdUserService;
import com.ysx.admin.vo.AdUserVo;
import com.ysx.common.constants.SystemConstants;
import com.ysx.common.pojo.TokenJsonVo;
import com.ysx.common.pojo.TokenRole;
import com.ysx.common.pojo.UserTokenInfo;
import com.ysx.common.utils.AppJwtUtil;
import com.ysx.common.utils.JwtToutiaoUtil;
import com.ysx.exception.ToutiaoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 管理员用户信息表 服务实现类
 * </p>
 *
 * @author ljh
 * @since 2021-09-07
 */
@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService
{

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    protected AdUserMapper adUserMapper;

    @Override//登录系统成功签发token令牌
    public Map<String, Object> loginSystemReturnToken(AdUserVo adUserVo) throws ToutiaoException {
     //思路：
        //1,判断对象输入的用户名或密码是否为Null
        String username = adUserVo.getName();
        String password = adUserVo.getPassword();
        if (username == null || password == null) {
            //2,为null 抛出自定义异常信息
            throw new ToutiaoException("您的账号密码不能为空");
        }
        //3,不为null 根据用户名查询数据库
        QueryWrapper<AdUser> adUserQueryWrapper = new QueryWrapper<>();
        adUserQueryWrapper.eq("name",username);
        AdUser adUserDB = adUserMapper.selectOne(adUserQueryWrapper);
        //4,判断返回的对象是否为Null
        if(ObjectUtils.isEmpty(adUserDB)){
            throw new ToutiaoException("您输入的账号或密码有误");
        }
        //6,根据数据库查询的密码 跟用户输入的密码 进行校验(加盐加密)
        String salt = adUserDB.getSalt();
        String saltPassword = password + salt;
        String passwordUser = DigestUtils.md5DigestAsHex(saltPassword.getBytes());//用户的密文
        if(!passwordUser.equals(adUserDB.getPassword())){
            throw new ToutiaoException("您输入的账号或密码有误");
        }
      /*  //7,封装数据
        String token = AppJwtUtil.createToken(Long.parseLong(adUserDB.getId() + ""));
        Map<String,Object> hm=new HashMap<>();
        hm.put("token",token);
        adUserDB.setPassword("");
        adUserDB.setSalt("");
        hm.put("userInfo",adUserDB);*/
        UserTokenInfo usertoken = new UserTokenInfo(
                Long.valueOf(adUserDB.getId()),
                adUserDB.getImage(),
                adUserDB.getNickname(),
                adUserDB.getName(),
                TokenRole.ROLE_ADMIN//管理员
        );
        TokenJsonVo token = JwtToutiaoUtil.createToken(usertoken);
        //存储到redis中  key: userId
        redisTemplate.opsForValue().set(
                SystemConstants.REDIS_TOKEN_ADMIN_PREFIX+adUserDB.getId(),
                token.getAccessToken(),JwtToutiaoUtil.TOKEN_TIME_OUT*5,
                TimeUnit.SECONDS);
        Map<String,Object> info = new HashMap<>();
        info.put("token",token);
        //设置为空字符串
        adUserDB.setPassword("");
        adUserDB.setSalt("");
        info.put("user",adUserDB);
        return info;
    }
}
